package com.langgan.base.mybase;


import android.content.pm.ActivityInfo;
import android.os.Bundle;

import androidx.annotation.LayoutRes;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProviders;

import com.alibaba.android.arouter.launcher.ARouter;
import com.gyf.barlibrary.ImmersionBar;

import java.lang.reflect.ParameterizedType;

/**
 * @author Beichen
 * @brief
 * @date 2021-12-01
 */
abstract public class BaseMvvmActivity<VM extends ViewModel,VDB extends ViewDataBinding> extends AppCompatActivity {
    protected VM mViewModel;
    protected VDB mViewDataBind;
    /***获取TAG的activity名称**/
//    protected final String TAG = this.getClass().getSimpleName();
    protected final String TAG = "XBC";
    private boolean isHorizontal = false;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHV();
        setContentView(getLayoutId());
        mViewDataBind = DataBindingUtil.setContentView(this, getLayoutId());
        mViewDataBind.setLifecycleOwner(this);
        //获得泛型参数的实际类型
        Class<VM> vmClass = (Class<VM>) ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
        mViewModel = ViewModelProviders.of(this).get(vmClass);
        initARouter();
        initImmersionBar();
        afterCreate();


    }

    /**
     * 设置横竖屏
     */
    private void setHV() {
        if (isHorizontal){
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }else{
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
        }
    }

    protected abstract void afterCreate();

    protected abstract @LayoutRes
    int getLayoutId();

    /**
     * 初始化Arouter路由
     */
    private void initARouter() {
        ARouter.getInstance().inject(this);
    }

    /**
     * 初始化沉浸式状态栏
     */
    private void initImmersionBar() {
        // 所有子类都将继承这些相同的属性,请在设置界面之后设置
        ImmersionBar.with(this).fitsSystemWindows(true).transparentStatusBar()
                .statusBarDarkFont(true).init();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 必须调用该方法，防止内存泄漏
        ImmersionBar.with(this).destroy();
    }

    public void setHorizontal(){
        isHorizontal = true;
        setHV();
    }
}

